// 联合类型，某个变量，可能是多种类型中的一种
interface Waiter {
  anjiao: boolean;
  say: () => {};
}
interface Teacher {
  anjiao: boolean;
  skill: () => {};
}
// 类型判断 as
function judgeWho(animal: Waiter | Teacher) {
  if (animal.anjiao) {
    (animal as Teacher).skill();
  } else {
    (animal as Waiter).say();
  }
}

// 属性判断 in
function judgeWhoTwo(animal: Waiter | Teacher) {
  if ("skill" in animal) {
    animal.skill();
  } else {
    animal.say();
  }
}

// 传统的JS类型判断 typeof
function add(first: string | number, second: string | number) {
  if (typeof first === "string" || typeof second === "string") {
    return `${first}${second}`;
  }
  return first + second;
}
// console.log(add(9, 6));

// 传统的JS类型判断 instanceof
class NumberObj {
  constructor(count: number) {
    this.count = count;
  }
  count: number;
}
class StringObj {
  constructor(str: string) {
    this.str = str;
  }
  str: string;
}
function addObj(first: StringObj | NumberObj, second: StringObj | NumberObj) {
  if (first instanceof NumberObj && second instanceof NumberObj) {
    return first.count + second.count;
  }
  return 0;
}
// const numObj1 = new NumberObj(1);
// const numObj2 = new NumberObj(2);
// console.log(addObj(numObj1, numObj2));
